﻿using PublicModels.APIObjs;
using SpatialLite.Core.API;
using SpatialLite.Core.Geometries;

namespace ChangchunPublicInfoWebSite.Common;

public class AnnotationReceiver
{
    public List<Location>? Points { get; set; }

    public List<List<Location>?>? Polylines { get; set; }

    public List<List<Location>?>? Polygons { get; set; }

    public List<Point> GetPoints() => Locations2Points(Points);

    public List<LineString> GetPolylines()
    {
        if (Polylines is null)
            return new List<LineString>();

        var lines= new List<LineString>(Polylines.Count);

        Polylines.ForEach((item) => lines.Add(new LineString(Locations2Positions(item))));

        return lines;
    }

    public List<Polygon> GetPolygons()
    {
        if (Polygons is null)
            return new List<Polygon>();

        var polygons = new List<Polygon>(Polygons.Count);

        Polygons.ForEach((item) => polygons.Add(new Polygon(Locations2Positions(item))));

        return polygons;
    }

    public List<Geometry> GetGeometries()
    {
        var list=new List<Geometry>();
        list.AddRange(GetPoints());
        list.AddRange(GetPolylines());
        list.AddRange(GetPolygons());
        return list;
    }

    public static List<Point> Locations2Points(List<Location>? locations)
    {
        if (locations is null)
            return new List<Point>();

        var result = new List<Point>(locations.Count);

        locations.ForEach((item) => result.Add(new Point(item.lng, item.lat)));

        return result;
    }

    public static CoordinateList Locations2Positions(List<Location>? locations)
    {
        if (locations is null)
            return new CoordinateList();

        var result = new CoordinateList();

        locations.ForEach((item) =>
        {
            result.Add(new Point(item.lng, item.lat).Position);
        });

        return result;
    }
}

